package novel.mzx.com.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;


import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;

import novel.mzx.com.R;
import novel.mzx.com.base.BaseActivity;

import novel.mzx.com.bean.CodeBean;
import novel.mzx.com.http.Api;
import novel.mzx.com.http.SpDataUtils;
import novel.mzx.com.utils.FinishActivity;
import novel.mzx.com.utils.JSONUtils;
import novel.mzx.com.utils.PermissionsUtil;
import novel.mzx.com.utils.SPUtil;
import novel.mzx.com.utils.SPUtils;
import novel.mzx.com.utils.StringUtils;
import novel.mzx.com.utils.ToastUtils;
import novel.mzx.com.views.InputMethodLayout;

/**
 * 登录页面处理
 * */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_phoneNum)
    EditText etPhoneNum;
    @BindView(R.id.et_password)
    EditText etPassword;

    @BindView(R.id.btn_login)
    Button btnLogin;

    @BindView(R.id.tv_regiest)
    TextView tv_regiest;


    @BindView(R.id.root)
    ScrollView scrollView;

    @BindView(R.id.rl)
    InputMethodLayout rl;

    int scrollViewHeight;



    @Override
    protected int getResourceId() {
        return R.layout.login_activity;
    }


    String update;


    @Override
    protected void initView() {
        super.initView();

        // android 4.4以上沉浸式以及bar的管理完成一些控件的初始化
        ImmersionBar.with(this).titleBar(R.id.toolbar).keyboardEnable(true).init();
        ImmersionBar.with(LoginActivity.this).keyboardEnable(false).statusBarDarkFont(true, 0.2f).navigationBarColor(R.color.btn3).init();

        String name = SPUtil.getInstance().getString("username", "");
        String role = SPUtil.getInstance().getString("role", "");
        // 如果之前输入过账号，重新打开时自动登录功能
        if(!name.equals("")){
            Intent intent = new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }



        // 设置软键盘状态监听的一些方法重写
        rl.setOnkeyboarddStateListener(new InputMethodLayout.onKeyboardsChangeListener() {
            @Override
            public void onKeyBoardStateChange(int state) {
                switch (state) {
                    case InputMethodLayout.KEYBOARD_STATE_HIDE:

                        break;
                    case InputMethodLayout.KEYBOARD_STATE_SHOW:

                        scrollView.post(new Runnable() {
                            @Override
                            public void run() {
                                scrollViewHeight = scrollView.getMeasuredWidth();

                                scrollView.scrollTo(0, 500);
                            }
                        });

                        break;

                    default:
                        break;
                }
            }
        });

        // 跳转到注册页面的逻辑
        tv_regiest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegiestActivity.class);
                startActivity(intent);
                FinishActivity.addActivity(LoginActivity.this);
            }
        });
    }



    @OnClick({ R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.btn_login:
                if (StringUtils.getEditTextData(etPhoneNum).isEmpty() && StringUtils.getEditTextData(etPassword).isEmpty()){
                    ToastUtils.showToast(this,"请输入用户名或密码");
                    return;
                }
                if(StringUtils.getEditTextData(etPhoneNum).isEmpty()){
                    ToastUtils.showToast(this,"请输入用户名");
                    return;
                }

                if(StringUtils.getEditTextData(etPassword).isEmpty()){
                    ToastUtils.showToast(this,"请输入密码");
                    return;
                }
                String uname = StringUtils.getEditTextData(etPhoneNum);
                String pwd = StringUtils.getEditTextData(etPassword);
                netWorkLogin(uname,pwd);



                break;


        }
    }


    private void netWorkLogin(String uname, String pwd) {

        try {
            OkGo.<String>get(Api.mainHost+Api.loginUrl)
                    .params("userAccount",uname)
                    .params("demoUserPwd",pwd)
                    .params("demoUserRole",2)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Log.e("login",response.body());

                            CodeBean codeBean = JSONUtils.parserObject(response.body(),CodeBean.class);
                            int code = codeBean.getResultCode();
                            if(code == 200){
                                Intent intent2 = new Intent(LoginActivity.this,MainActivity.class);
                                startActivity(intent2);

                                ToastUtils.showToast(LoginActivity.this,"登陆成功!");

                                SPUtil.getInstance().putString("memberId",codeBean.getData().getUserId()+"");
                                SPUtil.getInstance().putString("username",codeBean.getData().getUserName()+"");
                                SPUtil.getInstance().putString("account",codeBean.getData().getUserAccount()+"");
                                SPUtil.getInstance().putString("pwd",codeBean.getData().getUserPwd()+"");
                                SPUtil.getInstance().putString("role",codeBean.getData().getUserRole()+"");
                                SPUtil.getInstance().putString("userlogo",codeBean.getData().getUserImg()+"");
                                SPUtil.getInstance().putString("className",codeBean.getData().getClassName()+"");
                                finish();


                            }else {
                                ToastUtils.showToast(LoginActivity.this,"账号或密码错误，请重新登录!");
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);

                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




}
